/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.t4.irc.net;

import com.t4.irc.IRC;
import com.t4.irc.Logger;

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * IRCSocketFactory
 * @author T4_
 */
public class IRCSocketFactory {

    /** Logger */
    private Logger log = new Logger(this.getClass());

    /** The socket */
    private Socket socket;

    /** The input stream */
    private InputStream inputStream;

    /** The output stream */
    private OutputStream outputStream;

    /**
     * Creates a new socket
     * @throws Exception
     */
    public IRCSocketFactory() throws Exception {
        if(IRC.getConfiguration().isSSL()) {
            SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
            this.socket = (SSLSocket) sslsocketfactory.createSocket(IRC.getConfiguration().getServerAddress(), IRC.getConfiguration().getServerPort());
        } else {
            this.socket =  new Socket(IRC.getConfiguration().getServerAddress(), IRC.getConfiguration().getServerPort());
        }
        this.outputStream = socket.getOutputStream();
        this.inputStream = socket.getInputStream();
    }

    /**
     * Gets the socket
     * @return
     */
    public Socket getSocket() {
        return socket;
    }

    /**
     * Gets the input stream
     * @return
     */
    public InputStream getInputStream() {
        return inputStream;
    }

    /**
     * Gets the output stream
     * @return
     */
    public OutputStream getOutputStream() {
        return outputStream;
    }
}
